package  com.puhui.lib.widgets

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.view.Gravity
import androidx.annotation.ColorInt
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.ContextCompat
import com.blankj.utilcode.util.SizeUtils
import com.puhui.lib.R

/**
 * Description: 任意圆角TextView
 * Data：2020/6/5-9:37
 * Author: TangJian
 */
class RoundCornerTextView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = android.R.attr.textViewStyle
) : AppCompatTextView(context, attrs, defStyleAttr) {

    companion object {
        private const val HORIZONTAL: Int = 1
        private const val VERTICAL: Int = 2
    }

    // 圆角半径
    private var radius = SizeUtils.dp2px(5f).toFloat()

    // 按钮正常时的背景颜色
    private var startColor: Int
    private var endColor: Int

    // 不可点击状态颜色
    private var unableColor: Int

    private var colorOrientation: Int

    //背景边框
    private var isNeedFrame = false  //是否需要边框
    private var rect: RectF? = null
    private var frameColor: Int = 0xf6f6f6  //边框颜色
    private var frameWidth: Float = 1f  //边框宽度
    private var frameCornerRadius: Float = radius //边框圆角
    private lateinit var framePaint: Paint  //边框画笔

    //颜色渐变方向
    private var orientation = GradientDrawable.Orientation.LEFT_RIGHT

    init {
        val typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.RoundCornerTextView)
        radius = typedArray.getDimension(
            R.styleable.RoundCornerTextView_tvCornerRadius,
            resources.getDimension(R.dimen.space_180)
        )
        frameCornerRadius = radius

        val defaultColor = ContextCompat.getColor(getContext(), R.color.bg_color_f6)
        startColor = typedArray.getColor(R.styleable.RoundCornerTextView_startColor, defaultColor)
        endColor = typedArray.getColor(R.styleable.RoundCornerTextView_endColor, 0)
        unableColor = typedArray.getColor(R.styleable.RoundCornerTextView_unableColor, 0)

        colorOrientation = typedArray.getInt(R.styleable.RoundCornerTextView_tvOrientation, HORIZONTAL)

        frameColor = typedArray.getColor(R.styleable.RoundCornerTextView_tvFrameColor, defaultColor)
        frameWidth = typedArray.getDimension(R.styleable.RoundCornerTextView_tvFrameWidth, 1f)
        isNeedFrame = typedArray.getBoolean(R.styleable.RoundCornerTextView_isNeedFrame, false)

        typedArray.recycle()

        gravity = Gravity.CENTER

//        autoSizeStepGranularity: 每次改变的尺寸阶梯
//        autoSizeTextType="uniform": 设置TextView大小设置样式为支持改变(none时为不支持改变)
//        @SuppressLint("RestrictedApi")
//        setAutoSizeTextTypeUniformWithConfiguration(10, 20, 1, TypedValue.COMPLEX_UNIT_DIP)

        if (colorOrientation == VERTICAL) {
            orientation = GradientDrawable.Orientation.TOP_BOTTOM
        }

        if (unableColor == 0) {
            setBackColor(startColor, endColor)
        } else {
            setBackColor(unableColor)
        }
    }

    fun setBackColor(@ColorInt startColor: Int, @ColorInt endColor: Int = 0) {
        //创建无长度数组： var result = arrayOf<Int>()    var result = intArrayOf()
        //创建已知长度数组： var result =  Array<Int>(10)    var result = IntArray(10)
        val colors = if (endColor == 0) intArrayOf(startColor) else intArrayOf(startColor, endColor)

        val gradientDrawable = GradientDrawable(orientation, colors)
        gradientDrawable.cornerRadius = radius
        gradientDrawable.setColor(startColor)
        background = gradientDrawable
    }

    override fun setEnabled(enabled: Boolean) {
        //super.setEnabled(enabled)已经有判断了，这里只是为了避免执行下面的setBackColor代码
        if (isEnabled == enabled) return

        super.setEnabled(enabled)

        if (enabled) {
            setBackColor(startColor, endColor)
        } else {
            if (unableColor == 0) setBackColor(startColor, endColor) else setBackColor(unableColor)
        }
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        if (isNeedFrame) {
            val temp = frameWidth
            rect = RectF(temp, temp, measuredWidth.toFloat() - temp, measuredHeight.toFloat() - temp)

            framePaint = Paint()
            framePaint.color = frameColor
            framePaint.strokeWidth = frameWidth
            framePaint.isAntiAlias = true
            framePaint.style = Paint.Style.STROKE

            if (frameCornerRadius > measuredHeight / 2f) {
                frameCornerRadius = measuredHeight / 2f
            }
        }
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        if (isNeedFrame) {
            rect?.let {
                canvas?.drawRoundRect(it, frameCornerRadius, frameCornerRadius, framePaint)
            }
        }
    }
}